<?php
namespace Home\Controller;
use Think\Controller;
header("Content-type:text/html;charset=utf-8");
class ExamController extends BaseController {
   
   /*
		绩效考核评分
	 */
	public function mark(){
		//年份
		$year = $_GET['year'];
		//新加月份
		$month = $_GET['mtime'];
		//新加季度
		$quarter = $_GET['quarter'];
		// var_dump($_GET);die;
		$score_id = $_GET['score_id']; // 考核人员
		// var_dump($score_id);die;
		$p_id = $_GET['p_id']; // 考核项目
		$time = $_GET['time']; // 考核时间
        $ru_time = date("Y",$time);
		$scorearr = M('score')->where('id='.$score_id)->find();
		$kworker = M('worker')->where('worker_id='.$scorearr['worker_id'])->find();
		$posts_id = $kworker['posts_id']; // 考核人员岗位id
		$posts_name = M('posts')->where('posts_id='.$posts_id)->find();
		$p_name = $posts_name['posts_name'];
		$perfor = M('performance')->where('p_id='.$p_id)->find();
		// var_dump($perfor);die;
		$this->assign('kworker',$kworker);
		$this->assign('perfor',$perfor);

		//获取该id下的所有子分类
		$per = M('performance')->where('parent_id='.$p_id)->order('p_id asc')->select();
		// var_dump($per);die;
		if(!$per){
			$per = M('performance')->where('p_id='.$p_id)->select();
		}
        // var_dump($per);die;
		// 获取该id下的所有子分类的最终得分
		foreach($per as $key2 => $val2){
			$kaohearr[] = $val2['p_id'];
		}
		$map['ktime'] = $time; // 考核时间
		$map['kaohe_id'] = $score_id; // 考核人员id
        $map['ru_id'] = array('in',$kaohearr);
        // var_dump($map);die;
        $scoredetail = M('scoredetail')->where($map)->order('ru_id asc')->select();
		foreach($per as $key=>$val){
            $mp['parent_id'] = $val['p_id'];
            $mp['ktime'] = $ru_time;
            $rulea = M('rule')->where($mp)->select();
            if(empty($rulea)){
                $ru = M('rule')->order('ktime desc')->select();
                foreach($ru as $k => $v){
                    $rtime[] = $v['ktime'];
                }
                $rtime1 = array_unique($rtime);
                $mp1['parent_id'] = $val['p_id'];
                $mp1['ktime'] = $rtime1['0'];
                $per[$key]['rulelist'] = M('rule')->where($mp1)->select();
            }else{
                $per[$key]['rulelist'] = M('rule')->where($mp)->select();
            }
            foreach($scoredetail as $key4 => $val4){
                if($val['p_id'] == $val4['ru_id']){
                    $per[$key]['cha'] = $val4['cha'];
                }
            }
        }
		// if($p_id == '9' || $p_id == '10'){
		// 	if(empty($scoredetailarr['0'])){
		// 		$per['0']['cha'] = 0;
		// 	}
		// }
		if($p_id == '9' || $p_id == '10'){
			if(empty($scoredetail)){
				$per['0']['cha'] = 0;
			}
		}
		if($p_id == '7'){
			$score1 = $scorearr['public'];
		}elseif($p_id == '8'){
			$score1 = $scorearr['part'];
		}elseif($p_id == '9'){
			$score1 = $scorearr['price'];
		}elseif($p_id == '10'){
			$score1 = $scorearr['check'];
		}
	    // echo "<pre>";
	    // var_dump($per);
	    // echo "</pre>";die;
	    //新加
	    $this->assign('year',$year);
		$this->assign('month',$month);
		$this->assign('quarter',$quarter);
	    //
		$this->assign('p_id',$p_id);
		$this->assign('score',$per);
		$this->assign('score_id',$score_id);
        $this->assign('time',$time);
        $this->assign('score1',$score1);
		$this->display();
	}

	/*
		工作业绩考核
	 */
	public function gzyj(){
		// var_dump($_GET);die;
		$score_id = $_GET['score_id']; // 考核人员id
		$p_id = $_GET['p_id']; // 考核项目
		$time = $_GET['time']; // 考核时间
        $ru_time = date("Y",$time);
		$scorearr = M('score')->where('id='.$score_id)->find();
		// var_dump($scorearr);die;
		$worker_id = $scorearr['worker_id'];
		$kworker = M('worker')->where('worker_id='.$scorearr['worker_id'])->find();
		$posts_id = $kworker['posts_id']; // 获取考核人员岗位id
		$posts_name = M('posts')->where('posts_id='.$posts_id)->find();
		$po_name = $posts_name['posts_name']; // 岗位名称
		$map['p_name'] = array('like',$po_name);
		$perarr = M('performance')->where($map)->find();
		$pp_id = $perarr['p_id']; // 从performance表中获取岗位id
		// 获取该id下的所有子分类
		$per = M('performance')->where('parent_id='.$pp_id)->order('p_id asc')->select();
		if(!$per){
			$per=M('performance')->where('p_id='.$pp_id)->order('p_id asc')->select();
			// var_dump($per);die;
		}

		// 获取该id下的所有子分类的最终得分
		foreach($per as $key2 => $val2){
			$kaohearr[] = $val2['p_id'];
		}
		$map1['ktime'] = $time;
		$map1['kaohe_id'] = $score_id;
        $map1['ru_id'] = array('in',$kaohearr);
        $scoredetail = M('scoredetail')->where($map1)->order('ru_id asc')->select();
        // echo "<pre>";
        // var_dump($per);
        // echo "</pre>";die;
		foreach($per as $key => $val){
            $mp['parent_id'] = $val['p_id'];
            $mp['ktime'] = $ru_time;
            // $rulea = M('rule')->where($mp)->select();
            // if(empty($rulea)){
            //     $ru = M('rule')->order('ktime desc')->select();
            //     foreach($ru as $k => $v){
            //         $rtime[] = $v['ktime'];
            //     }
            //     $rtime1 = array_unique($rtime);
            //     $mp1['parent_id'] = $val['p_id'];
            //     $mp1['ktime'] = $rtime1['0'];
            //     $per[$key]['rulelist'] = M('rule')->where($mp1)->select();
            // }else{
            //     $per[$key]['rulelist'] = M('rule')->where($mp)->select();
            // }
            foreach($scoredetail as $key4 => $val4){
                if($val['p_id'] == $val4['ru_id']){
                    $per[$key]['cha'] = $val4['cha'];
                }
            }
		}
	 //    echo "<pre>";
	 //    var_dump($per);
		// echo "</pre>";die;

		$perfor = M('performance')->where('p_id='.$p_id)->find();
		// var_dump($perfor);die;
		$this->assign('kworker',$kworker);
		$this->assign('perfor',$perfor);
		$this->assign('score',$per);
		$this->assign('p_id',$p_id);
		$this->assign('score_id',$score_id);
		$this->assign('posts_id',$posts_id);
		$this->assign('worker_id',$worker_id);
        $this->assign('time',$time);
        $this->assign('scorearr',$scorearr);
		$this->display();

	}

	/*
		考核明细加减分原因列表 前四项+司法行政人员工作业绩
	 */
	public function markdetail_list(){
		// var_dump($_GET);die;
		//新加
		$year = $_GET['year'];
		$month = $_GET['month'];
		$quarter = $_GET['quarter'];
		//
		$score_id = $_GET['score_id']; // 考核人员ID
		$ru_id = $_GET['ru_id']; // 考核类别ID
		$ktime = $_GET['ktime']; // 考核时间
		$p_id = $_GET['p_id']; // 考核项目
        $number = $_GET['number'];
        //如果有月份
		if($month != '')
		{
			if($month == '1'){
				$ctime1 = strtotime($year .'-'. $month); // 要查看的年-月
			}else{
				for($i = 1;$i <= $month;$i++){
					$a[] = $i;
				}
				foreach($a as $k => $v){
					$ctime1[] = strtotime($year .'-'. $v);
				}
			}
		}
		//如果有季度

		if($quarter != ''){
				if($quarter == '1'){
				for($i = 1;$i <= 3;$i++){
					$a[] = $i;
				}
				foreach($a as $k => $v){
					$ctime1[] = strtotime($year .'-0'. $v);
				}
			}elseif($quarter == '2'){
				for($i = 1;$i <= 6;$i++){
					$a[] = $i;
				}
				foreach($a as $k => $v){
					$ctime1[] = strtotime($year . '-0' . $v);
				}
			}elseif($quarter == '3'){
				for($i = 1;$i <= 9;$i++){
					$a[] = $i;
				}
				foreach($a as $k => $v){
					$ctime1[] = strtotime($year . '-0' . $v);
				}
			}elseif($quarter == '4'){
				for($i = 1;$i <= 12;$i++){
					$a[] = $i;
				}
				foreach($a as $k => $v){
					$ctime1[] = strtotime($year . '-' . $v);
				}
			}
		}
		//
		$performance1 = M('performance')->where('p_id='.$p_id)->find();
		$performance2 = M('performance')->where('p_id='.$ru_id)->find();
		$map['score_id'] = $score_id;
		$map['ru_id'] = $ru_id;
		$map['ktime'] = $ktime;
        $map['number'] = $number;
        
        if($month != '' || $quarter != ''){
        	$map['mtime'] = array('in',$ctime1);
        }     
        
		$count = M('detail')->where($map)->count();
		$list = M('detail')->where($map)->select();
		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->assign('ktime',$ktime); // 考核时间
		$this->assign('score_id',$score_id);
		$this->assign('ru_id',$ru_id);
		$this->assign('p_id',$p_id); // 考核项目
		$this->assign('performance1',$performance1);
		$this->assign('performance2',$performance2);
        $this->assign('number',$number);
		$this->display();
	}

	

} 